package com.moon.interface_api.utils;


import org.apache.catalina.core.ApplicationContext;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;


public class StringUtil {

	private static final Class collection = Collection.class;
	private static final Class iterator = Iterator.class;
	private static final Class map = Map.class;
	private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	
	public static String toString(Object obj) {
		try {
			return obj.toString();
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 把byte[]数组转换成十六进制字符串表示形式
	 *
	 * @param tmp
	 *            要转换的byte[]
	 * @return 十六进制字符串表示形式
	 */
	public static String byteToHexString(byte[] tmp) {
		if (tmp == null) {
			throw new NullPointerException();
		}
		int len = tmp.length;
		char str[] = new char[len * 2];
		int i = 0;
		for (byte b : tmp) {
			str[i * 2] = hexDigits[b >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
			str[i * 2 + 1] = hexDigits[b & 0xf]; // 取字节中低 4 位的数字转换
			i++;
		}
		return new String(str);
	}

	/**
	 * 返回手机号码 
	 */
	private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153,166,180,181,182,183,184,185,186,187,188,189,198,199".split(",");
	private static String getTel() {
		int index=getNum(0,telFirst.length-1);
		String first=telFirst[index];
		String second=String.valueOf(getNum(1,888)+10000).substring(1);
		String third=String.valueOf(getNum(1,9100)+10000).substring(1);
		return first+second+third;
	}

	/**
	 *@Author:chengliang
	 *@date 2018/11/28_11:54
	 *@param 
	 *@return 
	 * 获取[start,end]随机数
	 */
	public static int getNum(int start,int end) {
		return (int)(Math.random()*(end-start+1)+start);
	}

	/**
	 *@Author:chengliang
	 *@date 2018/11/28_11:54
	 *@param
	 *@return
	 * 获取[start,end]随机数
	 */
	public static long getNum(long start,long end) {
		return (long)(Math.random()*(end-start+1L)+start);
	}

	public static String getTelByNick(){
		String tel = getTel();
		return tel.substring(0, 3) + "****" + tel.substring(7, tel.length());
	}

	public static String getTelByNick(String phoneNum){
		return phoneNum.substring(0, 3) + "****" + phoneNum.substring(7, phoneNum.length());
	}

	/**
	 *@Author:chengliang
	 *@date 2018/12/13_16:48
	 *@param
	 *@return
	 * url解码 一次
	 */
	public static String UrlDecoderOne(String url,String enc){
		String s1;
		try {
			s1 = URLDecoder.decode(url,enc);
		}catch (Exception e){
			return null;
		}
		return s1;
	}
	
	/**
	 *@Author:chengliang
	 *@date 2018/12/13_16:48
	 *@param 
	 *@return 
	 * url解码 两次
	 */
	public static String UrlDecoderTwo(String url,String enc){
		String s1;
		String s2;
		try {
			s1 = URLDecoder.decode(url,enc);
			s2 = URLDecoder.decode(s1,enc);
		}catch (Exception e){
			return null;
		}
		return s2;
	}
	
	/**
	 *@Author:chengliang
	 *@date 2018/12/13_16:55
	 *@param 
	 *@return 
	 * URL编码
	 */
	public static String UrlEncode(String url,String enc){
		String s1;
		try {
			s1 = URLEncoder.encode(url, enc);
		}catch (Exception e){
			return null;
		}
		return s1;
	}
	
	
	public static void main(String[] args){
	 	String url = "https://ds.alipay.com/?from=mobilecodec&scheme=alipayqr%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%253A%252F%252Fopenauth.alipay.com%252Foauth2%252FpublicAppAuthorize.htm%253Fapp_id%253D2018051560098638%2526scope%253Dauth_base%2526redirect_uri%253Dhttps%253A%252F%252Fwww.zmpay.top%252Fxpay%252Fjspay%252FX072034120181213161539891063";
       // ApplicationContext applicationContext = new ApplicationContext();
        String s = UrlDecoderTwo(url, "utf-8");
		System.out.println(s);
		String s1 = UrlEncode(s,"utf-8");
		String s2 = UrlEncode(s1,"utf-8");
		System.out.println(s1);
		System.out.println(s2);
	}
	
}
